/*
"Replication Data for: The real-time impact of the war on Russian imports: 
a synthetic control method approach" by Mancini, Conteduca, Borin 

Fig6b-7.do: The files export figure 6a and the underlying data to a dta file and 
export the data underlying Figure 7 to a dta file.
*/


clear  
 
  
 use "${main_loc}/data_toTurkeyHT_WTR",replace
 
 
drop if partner=="Ukraine"	
drop if partner=="Kazakhstan"	
drop if partner=="Belarus"	
drop if partner=="Serbia"	
drop if partner=="Armenia"
drop if partner=="Uzbekistan"
drop if partner=="Kyrgyzstan"
drop if partner=="Georgia"
drop if partner=="Azerbaijan"

levelsof iso2_o, local(levels) 

foreach c of local levels{
preserve

keep if iso2_o=="`c'"

 

	di "`c'"
 merge n:1 reporter partner using "${main_loc}/export_similarities_Turkey_tech_WTR.dta"
capture drop s1_* s2_* s3_* s4_* s5_*  
drop if _m!=3
replace s1=0 if s1==.
replace s2=0 if s2==.
replace s3=0 if s3==.
replace s4=0 if s4==.
replace s5=0 if s5==.

egen id=group(iso2_d),label
egen t=group(date)
tsset id t

drop if BVlndist==.
drop if BVcontiguity==.
drop if BVcommon_language==.
drop if lnagdp_wdi_const==.
 drop if lnavg_outp==.

tsfill,full
replace value=0 if value==.

sort id t
local varl  BVlndist BVcontiguity BVcommon_language lnagdp_wdi_const  max_agree_pta_goods max_agree_cu  lnavg_outp
foreach x of local varl {  
sort id `x' 
by id: carryforward `x' ,replace
}

 
sort id t
gen date_new=date  
order id date date_new 

bys id: replace date_new=l.date_new+1 if date_new==.
bys id: replace date_new=l2.date_new+2 if date_new==.
bys id: replace date_new=l3.date_new+3 if date_new==.
bys id: replace date_new=l4.date_new+4 if date_new==.
bys id: replace date_new=l5.date_new+5 if date_new==.
bys id: replace date_new=f.date_new-1 if date_new==.
bys id: replace date_new=f2.date_new-2 if date_new==.
bys id: replace date_new=f3.date_new-3 if date_new==.
bys id: replace date_new=f4.date_new-4 if date_new==.
bys id: replace date_new=f5.date_new-5 if date_new==.
format date_new %tm
drop date fdate year  month
rename date_new date

gen fdate = dofm(date)
gen year=year(fdate)
gen month=month(fdate)
gen dm=month



 
 keep if t>=13

sort id t

summ id if partner=="Turkey"
capture   synth   value   value BVlndist BVcontiguity BVcommon_language  lnavg_outp  s1 s2  s3  s4  max_agree_pta_goods ,   trunit("`r(mean)'") trperiod(38)    keep("`c'_Turkey_ht",replace) 
 summ id if partner=="Turkey"
capture synth_asmd, trunit("`r(mean)'") trperiod(38) depvar(value) data("`c'_Turkey_ht.dta")
sca `c'_Turkey_mean=r(mean)
sca test=r(mean)
gen soglia=0.4
sca `c'_Turkey_max=r(max)
sum soglia
if test>r(mean) {
capture erase "`c'_Turkey_ht.dta"  
 }

 restore
}


 

 foreach c of local levels {   
    clear 
capture use "${main_loc}/`c'_Turkey_ht.dta"
capture keep _Y_treated _Y_synthetic _time
capture gen iso2_o="`c'"
capture drop if iso2_o==""
capture drop if _time==.

capture save  "${main_loc}/`c'_pre_append_Turkey_ht.dta",replace
}
  
 
  

 
 
***************FIGURE 7a  
clear
cd "${main_loc}/"
local theFiles: dir . files "*_pre_append_Turkey_ht.dta" 

append using `theFiles' 
 
*as soon as I have intra EU in June, modify with 42
**********CHANGE HERE IF UDATE!!**********
gen keep=1 if _time==48
bys iso2_o: egen insample=max(keep)
drop if insample==.
drop if _time>50
egen id=group(iso2_o)
tsset id _time
 
drop if iso2_o=="UA"

rename iso2_o iso2
merge n:1 iso2 using "${main_loc}/sanctioning_list_WTR.dta" 
drop if _m == 2
drop _m
replace sanctioning=0 if sanctioning==.
collapse (sum) _Y_treated _Y_synthetic,by(_time sanctioning)
reshape wide _Y_treated _Y_synthetic, i(_time) j(sanctioning)
tsline _Y_treated1 _Y_synthetic1
keep _time _Y_treated1 _Y_synthetic1
 graph export "${main_loc}/Figure_6b.png",replace
save "${main_loc}/Figure_6b.dta",replace

 
 
 
  ***************FIGURE 7b  

clear
cd "${main_loc}/"
local theFiles: dir . files "*_pre_append_Turkey_ht.dta" 

append using `theFiles' 
 
*as soon as I have intra EU in June, modify with 42
**********CHANGE HERE IF UDATE!!**********
gen keep=1 if _time==48
bys iso2_o: egen insample=max(keep)
drop if insample==.
drop if _time>50
egen id=group(iso2_o)
tsset id _time
 
drop if iso2_o=="UA"
 
***FIGURE 1 and 2
rename iso2_o iso2
merge n:1 iso2 using "${main_loc}/sanctioning_list_WTR.dta" 
drop if _m == 2
drop _m
replace sanctioning=0 if sanctioning==.
keep if sanctioning==1
gen  period=1 if _time>=46 & _time<=48
drop if period!=1
collapse (sum) _Y_treated _Y_synthetic,by(iso2)
gen diff=(_Y_treated-_Y_synthetic)/1000000000
gen diffp=_Y_treated/_Y_synthetic*100-100
keep diff* iso2
 save "${main_loc}/Figure_7.dta",replace





 
 
